<?php

namespace app\controller;

use app\services\Jwt;
use app\services\SmsService;
use plugin\admin\app\model\Bank;
use plugin\admin\app\model\Good;
use plugin\admin\app\model\Order;
use plugin\admin\app\model\PriceLog;
use plugin\admin\app\model\User;
use support\Cache;
use support\Request;

class IndexController
{
    public function index(Request $request)
    {
        static $readme;
        if (!$readme) {
            $readme = file_get_contents(base_path('README.md'));
        }
        return $readme;
    }

    public function view(Request $request)
    {
        return view('index/view', ['name' => 'webman']);
    }

    public function json(Request $request)
    {
        return json(['code' => 0, 'msg' => 'ok']);
    }

    public function detail(Request $request)
    {
        $id = $request->get('id');
        $goods = Good::where('id',$id)->first();
        dd($goods);

    }

    public function mobileLogin(Request $request)
    {
        $mobile = $request->post('mobile');
        if (empty($mobile)){
            return json(['code'=>1,'msg'=>'手机号不能为空']);
        }
        $code = $request->post('code');
        if (empty($code)){
            return json(['code'=>1,'msg'=>'验证码不能为空']);
        }
        // 判断验证码是否正确
        $sms_code = Cache::get($mobile.'sms_code');
        if (empty($sms_code)){
            return json(['code'=>1,'msg'=>'请先获取验证码']);
        }
        if ($sms_code != $code){
            return json(['code'=>1,'msg'=>'验证码不正确']);
        }
        $user = User::where('mobile',$mobile)->first();
        if (empty($user)) {
            $user = User::create([
                'mobile' => $mobile,
                'nickname' => uniqid(),
                'password' => md5(123456),
                'username' => $mobile,
            ]);
        }
        $token = Jwt::createjwt($user->id);
        return json(['code'=>0,'msg'=>'登录成功','data'=>$token]);
    }

    public function sendSms(Request $request)
    {
//        $token = $request->header('token','1111');
//        $res = Jwt::verifyjwt($token);
//        if (!$res['status']){
//            return json(['code'=>9999,'msg'=>$res['msg']]);
//        }
        $mobile = $request->post('mobile');
        if (empty($mobile)){
            return json(['code'=>1,'msg'=>'手机号不能为空']);
        }
        $res = SmsService::sendSms($mobile);
        return json(['code'=>0,'msg'=>$res]);
    }

    public function bindingMobile(Request $request)
    {
        $token = $request->header('token','1111');
        $res = Jwt::verifyjwt($token);
        if (!$res['status']){
            return json(['code'=>9999,'msg'=>$res['msg']]);
        }
        $mobile = $request->post('mobile');
        if (empty($mobile)){
            return json(['code'=>1,'msg'=>'手机号不能为空']);
        }
        $code = $request->post('code');
        if (empty($code)){
            return json(['code'=>1,'msg'=>'验证码不能为空']);
        }
        // 判断验证码是否正确
        $sms_code = Cache::get($mobile.'sms_code');
        if (empty($sms_code)){
            return json(['code'=>1,'msg'=>'请先获取验证码']);
        }
        if ($sms_code != $code){
            return json(['code'=>1,'msg'=>'验证码不正确']);
        }
        $user = User::where('mobile',$mobile)->where('id','!=',$res['id'])->first();
        if ($user){
            return json(['code'=>1,'msg'=>'手机号已被绑定']);
        }
        User::updateOrCreate(
            ['id' => $res['id']],
            [
                'mobile' => $mobile,
            ]
        );
        return json(['code'=>0,'msg'=>'绑定成功']);
    }

    public function carOrder(Request $request)
    {
        $goods_id = $request->get('goods_id');
        $order = $this->orderData($goods_id);
        $reward_order_list = \think\facade\Db::table('wa_order')->where('is_reward',1)->paginate(10);
        $page = $reward_order_list->render();
        return  view('index/vipOrder', ['order'=>$order,'reward_order_list'=>$reward_order_list,'page'=>$page]);

    }

    public function orderData($goods_id=1)
    {
        $where =['is_reward' => 1];
        $where1 = [
            'goods_id' => $goods_id
        ];
        $where2 = [
            'is_record' => 1,
        ];
        // 实际订单数量
        $result['vip_count'] = Order::where($where1)->where($where2)->count();
        // 已互助数量
        $result['reward_count'] = Order::where($where1)->where($where2)->where($where)->count();
        // 待发放奖励订单数量
        $result['reward_order_count'] =  Order::where($where1)->where($where2)->where('is_reward',0)->count();
        // 已互助金额
        $result['reward_price'] = Order::where($where1)->where($where2)->where($where)->sum('reward_total_price');
//        $result['order_total_price'] = VipOrder::sum('price');
//        $result['order_total_price'] = RequestService::priceFormat($result['order_total_price']);
//        $result['reward_price'] = RequestService::priceFormat($result['reward_price']);
        $reward_order = Order::where('goods_id',$goods_id)->where('is_reward',0)->where('is_record',1)->orderBy('id')->first();
        if (!empty($reward_order)){
            $reward_order['customer_number'] = mask_string($reward_order->mobile);
            $reward_order['reward_total_price'] = priceFormat($reward_order['reward_total_price']);
        }
        $total_price = PriceLog::where('goods_id',$goods_id)->value('price')??'0.00';
        $result['progress'] = $total_price>0?bcdiv($total_price,$reward_order['reward_total_price'],2)*100 .'%':'0%';

        $result['reward_order'] = $reward_order;
        $result['total_price'] = $total_price;
        return $result;
    }


    public function bankList()
    {
        $list = Bank::select()->get()->toArray();
        return json(['code'=>0,'msg'=>'ok','data'=>$list]);
    }

}
